package com.gxa.xc.rbac.exception;

import com.gxa.xc.rbac.dto.ResultDTO;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author:IT-CNLM
 * @date:Created at 2020/10/27
 */
@ControllerAdvice
public class SystemExceptionHandler {


    /**
     *
     * 捕获没有权限返回页面的异常
     * @param e
     * @return
     */
    @ExceptionHandler(PermissionPageException.class)
    public ModelAndView permissionPageExceptionHandler(PermissionPageException e){

        ModelAndView modelAndView = new ModelAndView();
        // 将数据保存到模型中
        modelAndView.addObject("code", e.getCode());
        modelAndView.addObject("msg", e.getMsg());
        // 设置 视图的名字
        modelAndView.setViewName("403");

        return modelAndView;
    }




    /**
     * 捕获没有权限返回json的异常
     * @param e
     * @return
     */
    @ExceptionHandler(PermissionJsonException.class)
    @ResponseBody
    public ResultDTO permissionJsonExceptionHandler(PermissionJsonException e){
        return new ResultDTO(e.getCode(), e.getMsg());
    }



}
